package cn.chinaflame.pagodas.web;

import cn.chinaflame.pagodas.Result.Result;
import cn.chinaflame.pagodas.annotation.ApiDoc;
import cn.chinaflame.pagodas.dto.PaOrderDto;
import cn.chinaflame.pagodas.entity.PaOrder;
import cn.chinaflame.pagodas.enums.CodeEnum;
import cn.chinaflame.pagodas.repository.PaOrderRepository;
import cn.chinaflame.pagodas.req.PaOrderAddReq;
import cn.chinaflame.pagodas.req.PaOrderQryReq;
import cn.chinaflame.pagodas.service.CommonService;
import cn.chinaflame.pagodas.service.PaOrderService;
import cn.chinaflame.pagodas.utils.ParamPreconditions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import cn.chinaflame.pagodas.Result.Result;
import cn.chinaflame.pagodas.entity.Boy;
import cn.chinaflame.pagodas.repository.BoyRepository;
import cn.chinaflame.pagodas.service.BoyService;
import cn.chinaflame.pagodas.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
/**
 * 佛龛订单相关接口
 *
 * @author boming
 * @date 2018年6月21日 下午2:25:16
 */
@RestController
@Transactional(rollbackFor = Exception.class)
@RequestMapping(value = "/openapi/v1/pagodas/order/")
public class OpenPaOrderAPI {
    private static Logger log = LoggerFactory.getLogger(OpenPaOrderAPI.class);
    @Autowired
    private PaOrderService orderService;
    @Autowired
    private CommonService commonService;

    @Autowired
    private PaOrderRepository ywOrderRepository;

    /**
     * 查找订单
     *
     * @return
     */
//	@RestAction
    @PostMapping(value = "query/list")
    @ApiDoc(value = PaOrderQryReq.class, result = PaOrderDto.class)
    public Object findAllOrders(@RequestBody PaOrderQryReq req) {
        log.info("订单列表查询req:{}", req);
        if (commonService.paginationSupport(req.getPageIndex(), req.getPageSize()))
            return orderService.queryByPageByJdbc(req);
        return orderService.query(req);
    }

    /**
     * 查询一笔订单
     *
     * @param id 订单id
     * @return
     */
    @GetMapping(value = "query/{id}")
    @ApiDoc(Result.class)
    public Object findOrderById(@PathVariable String id) {
        log.info("查询单个订单id:{}", id);
        ParamPreconditions.checkNotEmpty(id, CodeEnum.FAIL.getCode(), "id为空");
        PaOrderDto dto = orderService.findOrderByid(id);
        ParamPreconditions.checkArgument(dto != null, CodeEnum.FAIL.getCode(), "没有这笔订单");
        return Result.create(dto);
    }

    /**
     * 删除订单（逻辑删除）
     *
     * @param id 订单id
     */
    @DeleteMapping(value = "delete/{id}")
    @ApiDoc(Result.class)
    public Object deleteOrder(@PathVariable("id") String id) {
        ParamPreconditions.checkNotEmpty(id, CodeEnum.FAIL.getCode(), "没有传订单id");
        PaOrder order = ywOrderRepository.findPaOrderById(id);
        ParamPreconditions.checkArgument(order != null, CodeEnum.FAIL.getCode(), "没有这笔订单");
        order.setPayType("N");
        return Result.create("删除成功");
    }

    /**
     * 下单接口
     */
    @PostMapping(value = "add")
    @ApiDoc(PaOrderAddReq.class)
    public Object addOrder(@RequestBody PaOrderAddReq req) {
        log.info("订单添加接口Req{}:", req);
        return orderService.addOrder(req);
    }

    @PostMapping(value = "qfd/add")
    public Object qfjOrderAdd(@RequestBody PaOrderAddReq req) {
        log.info("祈福金订单添加接口Req{}:", req);
        return orderService.qfjOrderAdd(req);
    }



    /**
     * 查询订单详情
     *
     * @param orderId 订单号或者订单详情ID
     * @return
     */
    @GetMapping(value = "query/orderitem")
    @ApiDoc(Result.class)
    public Object queryOrderItem(String orderId) {

        return orderService.queryOrderItem(orderId);
    }

    /**
     * 查询订单列表（平台后台）
     *
     * @param orderId
     * @return
     */
    @PostMapping(value = "query/list/new")
    @ApiDoc(PaOrderQryReq.class)
    public Object findAllOrdersNew(@RequestBody PaOrderQryReq req) {
        log.info("订单列表查询req:{}", req);
        if (commonService.paginationSupport(req.getPageIndex(), req.getPageSize())) {
            return orderService.queryByPageByJdbc2(req);
        }
        return orderService.query(req);
    }

    @GetMapping(value = "pmd")
    public Object pmdOrder () {
        log.info("订单跑马灯接口");
        return orderService.pmdOrder();
    }
/*	public Object queryOrderItem2(String orderId){
		ParamPreconditions.checkNotEmpty(orderId, CodeEnum.FAIL.getCode(), "订单号不能为空");
		
		return orderService.queryOrderItem2(orderId);
	}*/


}













